library(tidyverse)
library(caret)
library(gbm)
library(parallel)
library(doMC)

source("helpers_v2.R")
#load data
source("../load_data.R")
#generate_features of choice change
generate_features <- function(eventdata) {
  
  lapply(unique(eventdata$STUDENTID), function(sid) {
    
    stu_df <- eventdata %>%
      filter(STUDENTID == sid) %>%
      filter(Observable == "DropChoice")
    
    tstamps <-  stu_df$EventTime
    choice_texts <- stu_df$ExtendedInfo
    
    pmap(list(
      str_match_all(choice_texts, '\\"source\\"\\:\\"([1-4])\\",\\"target\\"\\:([1-4])'),
      tstamps), function(x, y) {
        mat <- x[, c(2, 3), drop = FALSE]
        colnames(mat) <- c("source", "target")
        as.data.frame(mat, stringsAsFactors = FALSE) %>%
          mutate(tstamp = y)
      }) %>%
      bind_rows() %>%
      mutate(STUDENTID = sid)
    
  }) %>%
    bind_rows() %>%
    as_tibble() %>%
    distinct() -> tmpdf
  
  tmpdf %>%
    arrange(STUDENTID, target, tstamp) %>%
    group_by(STUDENTID, target, source) %>%
    summarise(min_ts = min(tstamp)) %>%
    ungroup() %>%
    arrange(STUDENTID, target, min_ts) %>%
    group_by(STUDENTID, target) %>%
    mutate(next_ts = lead(min_ts)) %>%
    ungroup() %>%
    mutate(hiatus = as.numeric(next_ts - min_ts)) %>%
    replace_na(list(hiatus = 0)) %>%
    group_by(STUDENTID) %>%
    summarise(choicechange_tot_time = sum(hiatus)) %>%
    ungroup() -> df
  
  n_change_df <- tmpdf %>%
    select(STUDENTID, source, target) %>%
    distinct() %>%
    group_by(STUDENTID, source) %>%
    summarise(n_changes = n_distinct(target) - 1) %>%
    ungroup() %>%
    group_by(STUDENTID) %>%
    summarise(choicechange_tot_changes = sum(n_changes)) %>%
    ungroup()
  
  expand(eventdata, nesting(STUDENTID)) %>%
    left_join(df) %>%
    left_join(n_change_df) %>%
    replace_na(list(choicechange_tot_changes = 0,
                    choicechange_tot_time = 0))
  
}

test_features_choicechange <- generate_features(testBlockA)

train_features_choicechange <- generate_features(trainBlockA)

save(test_features_choicechange,train_features_choicechange,
     file = "features_choicechange.Rdata")
